Copyright Will Kew, 2016, 2017
This file is part of FTMS Visualisation (also known as i-van Krevelen).
FTMS Visualisation is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FTMS Visualisation is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FTMS Visualisation. If not, see <http://www.gnu.org/licenses/>.
This notebook will read in the data and show the full resolution spectra using datashader to downsample the data on the fly to the region of interest. It is simple to imagine how further functionality could be coupled with this. The code is provided purely as a proof of concept.
Prerequisites are listed below.
It has been tested on Python 3, and previous versions on python 2.
Code here is provided as a proof-of-concept rather than production level code.
Download from github - https://github.com/bokeh/datashader , extract the zip
In a command line/terminal, go to the extracted files and enter the command:
python setup.py install
Package Versions this code has been tested & works on:
You can install a working set of packages by installing Miniconda2 (http://conda.pydata.org/miniconda.html) and use the following command:
conda install bokeh datashader jupyter
In [1]:
import pandas as pd
import numpy as np
import xarray as xr
import datashader as ds
import datashader.transfer_functions as tf
from datashader.bokeh_ext import InteractiveImage
from IPython.core.display import HTML, display
import math, os
import bokeh
import bokeh.plotting as bp
import bokeh.palettes as bpal
from bokeh.plotting import figure, show, output_file, gridplot, hplot
from bokeh.models import HoverTool, ColumnDataSource, OpenURL, TapTool, Rect, Range1d
from bokeh.models.mappers import LinearColorMapper
from bokeh.models.widgets import Panel, Tabs
from collections import OrderedDict
In [2]:
import sys
print("Python version: " +sys.version)
print("Pandas version: " +pd.__version__)
print("Numpy version: " +np.__version__)
print("xarray version: " +xr.__version__)
print("Datashader version: " +ds.__version__)
print("Bokeh version: " +bokeh.__version__)
In [3]:
#Python 2 allows relative import:
import FTMSVizProcessingModule as FTPM
In [4]:
bp.output_notebook()
df = pd.read_csv('data/RawData/SRFA.xy',sep=" ",header=None,names=["mz","RelAbun"])
inputfile = "data/OutputCSV/SRFA-hits.csv"
isolist = "data/OutputCSV/SRFA-isohits.csv"
nohitslist = "data/OutputCSV/SRFA-nohits.csv"
x_range=(min(df["mz"]),max(df["mz"]))
y_range=(min(df["RelAbun"]),max(df["RelAbun"]))
options = dict(line_color='blue')
display(HTML("<style>.container { width:90% !important; }</style>"))
In [5]:
data,hetclassintdf = FTPM.mycsvreader(inputfile)
isodata = FTPM.isocsvreader(isolist)
nodata = FTPM.nohitsreader(nohitslist)
#data.tail(10)
#isodata.tail(10)
#nodata.tail(10)
In [6]:
glocmap = list(bpal.Viridis10)
glocmap2 = list(bpal.Inferno8)
glocmap.reverse()
glocmap2.reverse()
source = ColumnDataSource(data)
isosource = ColumnDataSource(isodata)
nosource = ColumnDataSource(nodata)
url = "http://www.chemspider.com/Search.aspx?q=@Formula"
TOOLS="resize,crosshair,pan,wheel_zoom,box_zoom,undo,redo,reset,tap,previewsave,box_select,poly_select,lasso_select,hover"
In [7]:
width = 1200
height = 600
def create_image(x_range=x_range, y_range=y_range, w=width, h=height):
cvs = ds.Canvas(x_range=x_range, y_range=y_range, plot_height=h, plot_width=w)
agg = cvs.line(df, 'mz', 'RelAbun', agg=ds.any())
return tf.shade(agg,cmap=["darkblue"])
def base_plot(tools='pan,wheel_zoom,reset'):
p = bp.figure(tools=TOOLS, plot_width=width, plot_height=height,
x_range=x_range, y_range=y_range, outline_line_color=None,
min_border=0, min_border_left=0, min_border_right=0,
min_border_top=0, min_border_bottom=0)
p.xgrid.grid_line_color = None
p.ygrid.grid_line_color = None
return p
In [8]:
p = base_plot()
p.scatter(x='mz', y='RA',source=source,fill_color='black',line_color=None)
hover = p.select(dict(type=HoverTool))
hover.tooltips = OrderedDict([('Formula', "@Formula"),('Mass',"@mz{1.11111}"),('Error (ppm)',"@Error{1.11}")])
p.scatter(x='mz', y='RA',source=isosource,fill_color='green',line_color=None)
p.scatter(x='mz', y='RA',source=nosource,fill_color='red',line_color=None)
taptool = p.select(type=TapTool)
taptool.callback = OpenURL(url=url)
InteractiveImage(p, create_image)
Out[8]:
In [ ]: